﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.WaterManagement
{
    /// <summary>
    /// 积分流水
    /// </summary>
    public class IntegralWaterController : BaseController
    {
        // 保存模型数据
        private static
            Dictionary<string, Model.View_Order_FlowInt> modelDic = new Dictionary<string, Model.View_Order_FlowInt>();
        private static
            List<Model.View_Order_FlowInt> modelList = new List<Model.View_Order_FlowInt>();
        // 操作员
        protected Model.View_OperUser operUser;

        // 加载商户
        public ActionResult InitMerID(JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.Order_Mer_T> page = new Page<Model.Order_Mer_T>();
                List<Model.Order_Mer_T> list = new List<Model.Order_Mer_T>();//定义商品分类设置
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = SRS.GetMer(user.OperID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        foreach (JToken item in jieguo)
                        {
                            Model.Order_Mer_T m = new Model.Order_Mer_T();
                            m.MerID = item["MerID"].ToString();
                            m.MerName = item["MerName"].ToString();
                            m.MerAdr = item["MerAdr"].ToString();
                            m.MerCall = item["MerCall"].ToString();
                            m.MerTxt = item["MerTxt"].ToString();
                            list.Add(m);
                        }
                    }
                    jqgridparam.records = list.Count;//查询的数据总数
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = page.FindListPage(list, ref jqgridparam)//返回当前页的数据
                    };
                    return Json(data);
                }
                return Json(searcho["Msg"].ToString());
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 查询所有用户平台积分信息
        /// </summary>
        /// <returns></returns>
        public List<Model.View_Order_FlowInt> GetGridData(string MerID, JqGridParam jqgridparam)
        {
            #region 加载表格
            Page<Model.View_Order_FlowInt> page = new Page<Model.View_Order_FlowInt>();
            List<Model.View_Order_FlowInt> list_UserInt = new List<Model.View_Order_FlowInt>();
            string result = SRS.GetOrder_FlowInt(jqgridparam.page, jqgridparam.rows,MerID, YZM, 1);  // 执行远程请求
            //if (!string.IsNullOrEmpty(result) && result.IndexOf("\"BS\":\"1\"") > -1)
            //{
                JObject search = JObject.Parse(result);//将服务返回的结果进行优化
                string bsMer = search["BS"].ToString();//获取返回结果是否正常标识
                if (bsMer == "1")//返回结果正常
                {
                    IList<JToken> jieguoMer = search["rows"].Children().ToList();//解析返回的结果
                    //List = ParseClass<Model.Order_Organization_T>.JsonList(jieguo);//解析返回的结果\
                    list_UserInt = ParseClass<Model.View_Order_FlowInt>.JsonList(jieguoMer);
                    #region
                    //if (jieguoMer.Count > 0)
                    //{
                    //    foreach (JToken item in jieguoMer)
                    //    {
                    //        Model.View_Order_FlowInt m = new Model.View_Order_FlowInt();
                    //        m.FlowIntID = item["FlowIntID"].ToString();
                    //        m.AccID = int.Parse(item["AccID"].ToString());
                    //        m.IntID = item["IntID"].ToString();
                    //        m.IntNowNub = int.Parse(item["IntNowNub"].ToString());
                    //        m.IntFlowType = item["IntFlowType"].ToString();
                    //        m.IntNowNub = int.Parse(item["IntNowNub"].ToString());
                    //        m.IntHaveNub = int.Parse(item["IntHaveNub"].ToString());
                    //        m.IntEndNub = int.Parse(item["IntEndNub"].ToString());
                    //        m.IntFlowTime = item["IntFlowTime"].ToString();
                    //        m.IntFlowOper = int.Parse(item["IntFlowOper"].ToString());
                    //        m.OrdrID = item["OrdrID"].ToString();
                    //        m.IntBZ = item["IntBZ"].ToString();
                    //        m.AccNum = item["AccNum"].ToString();
                    //        m.AccCall = item["AccCall"].ToString();
                    //        m.AccName = item["AccName"].ToString();
                    //        m.AccIdentifier = item["AccIdentifier"].ToString();
                    //        if ((string)item["AccSex"] == null)
                    //        {
                    //            m.AccSex = null;
                    //        }
                    //        else
                    //        {
                    //            m.AccSex = int.Parse(item["AccSex"].ToString());
                    //        }
                    //        //m.AccSex = int.Parse(item["AccSex"].ToString());
                    //        m.AccCerType = item["AccCerType"].ToString();
                    //        m.AccCerNub = item["AccCerNub"].ToString();
                    //        m.IdentityID = item["IdentityID"].ToString();
                    //        m.CategoryID = item["CategoryID"].ToString();
                    //        m.SataeID = item["SataeID"].ToString();
                    //        m.AccCerType_DicName_Login = item["AccCerType_DicName_Login"].ToString();
                    //        m.DepID = item["DepID"].ToString();
                    //        m.DepID_DepName_Department = item["DepID_DepName_Department"].ToString();
                    //        m.IdentityID_DiName_SystemIdentity = item["IdentityID_DiName_SystemIdentity"].ToString();
                    //        m.CategoryID_DicName_SystemIdentity = item["CategoryID_DicName_SystemIdentity"].ToString();
                    //        m.StateID_DicName_SystemIdentity = item["StateID_DicName_SystemIdentity"].ToString();
                    //        m.MerID = item["MerID"].ToString();
                    //        list_UserInt.Add(m);
                    //    }
                    //}
                    #endregion
                }
                return list_UserInt;
            #endregion
        }

       /// <summary>
       /// 加载表格
       /// </summary>
       /// <param name="MerID">商户ID</param>
       /// <returns></returns>
        public ActionResult InitTable(string MerID, JqGridParam jqgridparam)
        {
            #region 加载表格
            List<Model.View_Order_FlowInt> list_UserWalt = new List<Model.View_Order_FlowInt>();
            list_UserWalt = GetGridData(MerID, jqgridparam);
            jqgridparam.records = list_UserWalt.Count;//查询的数据总数
            var data = new
            {
                total = list_UserWalt.Count.ToString(),
                rows = list_UserWalt//返回当前页的数据
            };
            return Json(data, JsonRequestBehavior.AllowGet);
            #endregion
        }

        /// <summary>
        /// 查询用户商户积分信息
        /// </summary>
        /// /// <param name="MerID">商户ID</param>
        /// <param name="SearchAccName">姓名</param>
        /// <param name="SearchAccCall">手机号</param>
        /// <param name="SearchAccIdentifier">个人编号</param>
        /// <param name="SearchAccCerNub">证件号</param>
        /// <param name="SearchDepID">组织结构ID</param>
        /// <param name="jqgridparam">分页参数</param>
        /// <returns></returns>
        public ActionResult SeachTable(string MerID, string SearchAccName, string SearchAccCall, string SearchAccIdentifier,string SearchAccCerType, string SearchAccCerNub, string SearchDepID, JqGridParam jqgridparam)
        {
            #region 加载表格
            try
            {
                Page<Model.View_Order_FlowInt> page = new Page<Model.View_Order_FlowInt>();
                List<Model.View_Order_FlowInt> list_UserInt = new List<Model.View_Order_FlowInt>();
                list_UserInt = GetGridData(MerID, jqgridparam);
                if (list_UserInt.Count != 0)
                {
                    //姓名
                    if (SearchAccName != "")
                    {
                        list_UserInt = list_UserInt.Where(i => i.AccName.Contains(SearchAccName)).ToList();
                    }
                    //电话号
                    if (SearchAccCall != "")
                    {
                        list_UserInt = list_UserInt.Where(i => i.AccCall.Contains(SearchAccCall)).ToList();
                    }
                    //个人编号
                    if (SearchAccIdentifier != "")
                    {
                        list_UserInt = list_UserInt.Where(i => i.AccIdentifier.Contains(SearchAccIdentifier)).ToList();
                    }
                    //证件类型
                    if (SearchAccCerType != "")
                    {
                        list_UserInt = list_UserInt.Where(i => i.AccCerType==SearchAccCerType).ToList();
                    }
                    //证件号
                    if (SearchAccCerNub != "")
                    {
                        list_UserInt = list_UserInt.Where(i => i.AccCerNub.Contains(SearchAccCerNub)).ToList();
                    }
                    //部门ID
                    if (SearchDepID != "")
                    {
                        list_UserInt = list_UserInt.Where(i => i.DepID==SearchDepID).ToList();
                    }
                    jqgridparam.records = list_UserInt.Count;//查询的数据总数
                    var data = new
                    {
                        total = list_UserInt.Count.ToString(),
                        rows = list_UserInt
                    };
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = new
                    {
                        total = 0,
                        rows = list_UserInt
                    };
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");

            }
            #endregion
        }
        #region 获取证件类型
        /// <summary>
        /// 获取证件类型
        /// </summary>
        /// <returns></returns>
        public ActionResult getacccertype()
        {
            List<Basic_Dict_T> ls = new List<Basic_Dict_T>();
            ls = PublicMethod.GetDict.GetList(18);
            return Json(ls, JsonRequestBehavior.AllowGet);
        }
        #endregion
        /// <summary>
        /// 获取所有部门
        /// </summary>
        /// <returns></returns>
        public ActionResult getDep()
        {
            ZHHQSERVICE.Service1Client service = new ZHHQSERVICE.Service1Client();
            try
            {
                Model.View_OperUser operUser = Session[SessionStorage.UserLogin] as Model.View_OperUser;
                List<TreeJsonEntity> TreeList = new List<TreeJsonEntity>();
                string result = service.GetDepatMentNoValidate("", 1);
                Model.DataResult dr = ParseClass<Model.DataResult>.StrDeserialize(result, 1);
                List<Basic_Department_T> ls = new List<Basic_Department_T>();
                JObject jb = JObject.Parse(result);
                if (jb["BS"].ToString() == "1")//返回结果正常
                {
                    IList<JToken> jieguo = jb["rows"].Children().ToList();
                    ls = ParseClass<Basic_Department_T>.JsonList(jieguo);//解析返回的结果
                    foreach (Basic_Department_T item in ls)
                    {
                        bool hasChildren = false;
                        IList childnode = ls.FindAll(t => t.DepParentID == item.DepID);
                        if (childnode.Count > 0)
                        {
                            hasChildren = true;
                        }
                        TreeJsonEntity tree = new TreeJsonEntity();
                        tree.id = item.DepID;
                        tree.text = item.DepName;
                        tree.value = item.DepID;
                        tree.isexpand = true;
                        tree.complete = true;
                        tree.hasChildren = hasChildren;
                        tree.parentId = item.DepParentID;
                        TreeList.Add(tree);
                    }
                    string a = TreeList.TreeToJson();
                    return Content(TreeList.TreeToJson());
                }
                else
                {
                    return Json(jb["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        // 主页
        public ActionResult Index()
        {
            // 操作员
            operUser = Session[SessionStorage.OperUser] as Model.View_OperUser;
            if (operUser != null)
            {
                ViewBag.UserPowers = operUser.RolePower;
                // 身份类型
                ViewBag.AccCerTypeList = GetDicForDictID("18");
                // 部门
                List<SelectListItem> slis = new List<SelectListItem>();
                foreach (Basic_Department_T m in GetDepList(operUser))
                {
                    SelectListItem sli = new SelectListItem();
                    sli.Text = m.DepName;
                    sli.Value = m.DepID;
                    slis.Add(sli);
                }
                ViewBag.DepIDList = slis;
            }
            else
            {             
                return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面              
            }

            return View();
        }

    }
}
